package com.drinkian.authentication.handler;

import com.alibaba.fastjson.JSON;
import com.drinkian.entity.RequestResult;
import com.drinkian.utils.WebUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        RequestResult requestResult = new RequestResult(HttpStatus.UNAUTHORIZED.value(), "用户认证失败！");
        WebUtil.send(response, JSON.toJSONString(requestResult));
    }
}
